package cz.cacek.ebook.packager;

import j2me.util.ArrayList;
import j2me.util.Iterator;
import j2me.util.List;

import java.io.InputStream;
import java.io.OutputStream;

import javolution.xml.XMLBinding;
import javolution.xml.XMLFormat;
import javolution.xml.XMLObjectReader;
import javolution.xml.XMLObjectWriter;
import javolution.xml.stream.XMLStreamException;

public class ProfileBean {

	private List books;

	private String midletName;

	private String jadPath;


	public String getMidletName() {
		return midletName;
	}

	public void setMidletName(String midletName) {
		this.midletName = midletName;
	}

	public String getJadPath() {
		return jadPath;
	}

	public void setJadPath(String jadPath) {
		this.jadPath = jadPath;
	}


	public List getBooks() {
		return books;
	}

	public void setBooks(List books) {
		this.books = books;
	}

	public int getBookCount() {
		return this.books != null ? this.books.size() : 0;
	}

	public static String getOutPrefixBase(String outPrefix) {
		final String tmpPrefix = outPrefix;
		final int tmpPos = Math.max(tmpPrefix.lastIndexOf('/'), tmpPrefix
				.lastIndexOf('\\'));
		String prefixBase = tmpPos > 0 ? tmpPrefix.substring(tmpPos + 1)
				: tmpPrefix;
		return prefixBase;
	}

	public static String getOutPrefix(String path) {
		String outPrefix = path;
		if (outPrefix != null && outPrefix.lastIndexOf('.') != -1)
			outPrefix = outPrefix.substring(0, outPrefix.lastIndexOf('.'));
		return outPrefix;

	}

	public static String generatedMidletName(String jadPath) {
		String mName = null;
		if(jadPath != null){
			String outPrefix = getOutPrefix(jadPath);
			if( null != outPrefix ){
				String outPrefixBase = getOutPrefixBase(outPrefix);
				if(outPrefixBase != null){
					mName = "EBook_" +outPrefixBase;
				}
			}
		}
		return mName;
	}

	public static void write(ProfileBean a, OutputStream output)
			throws XMLStreamException {
		XMLObjectWriter writer = XMLObjectWriter.newInstance(output);
		writer.setBinding(createXmlBinding()); // Optional.
		writer.setIndentation("\t"); // Optional (use tabulation for
		// indentation).
		writer.write(a);
		writer.close();
	}

	public static ProfileBean read(InputStream input) throws XMLStreamException {
		XMLObjectReader reader = XMLObjectReader.newInstance(input);
		reader.setBinding(createXmlBinding());
		ProfileBean b = (ProfileBean) reader.read();
		reader.close();
		return b;
	}

	private static XMLBinding binding = null;

	private static XMLBinding createXmlBinding() {
		if (binding == null) {
			binding = new XMLBinding() {
				protected final XMLFormat BOOK_XML = new XMLFormat(Book.class) {

					public void read(InputElement xml, Object obj)
							throws XMLStreamException {
						Book a = (Book) obj;
						a.setName(xml.getAttribute("name", null));
						a.setId(xml.getAttribute("id", null));
						// a.setDescription(xml.getAttribute("description",
						// null));
						a.setPartSize(xml.getAttribute("partSize",
								Constants.PROP_PARTSIZE_DEF));
						a.setRightToLeft(xml.getAttribute("rightToLeft", a
								.isRightToLeft()));
						a.setFilepath(xml.getAttribute("filepath", null));
						a.setDescription(xml.getAttribute("description", null));
						// a.setDescription((String)xml.get("description",String.class));
						// a.setText((String)xml.get("text",String.class));
						a.setText(xml.getText().toString());

					}

					public void write(Object obj, OutputElement xml)
							throws XMLStreamException {
						Book a = (Book) obj;
						xml.setAttribute("id", a.getId());
						xml.setAttribute("name", a.getName());
						xml.setAttribute("partSize", a.getPartSize());
						xml.setAttribute("rightToLeft", a.isRightToLeft());
						xml.setAttribute("filepath", a.getFilepath());
						xml.setAttribute("description", a.getDescription());

						xml.addText(a.getText());

					}

				};

				protected final XMLFormat PROFILE_XML = new XMLFormat(
						ProfileBean.class) {

					public void read(InputElement xml, Object obj)
							throws XMLStreamException {
						ProfileBean a = (ProfileBean) obj;
						List bookList = new ArrayList();
						a.setMidletName(xml.getAttribute("midletName",null));
						a.setJadPath(xml.getAttribute("jadPath",null));
						int count = xml.getAttribute("count", 0);
						for (int i = 0; i < count; i++) {
							Book bk = (Book) xml.get("book",

							Book.class);
							bookList.add(bk);
						}
						a.setBooks(bookList);

						// a.setBooks((List)xml.get("books",ArrayList.class));
					}

					public void write(Object obj, OutputElement xml)
							throws XMLStreamException {
						ProfileBean a = (ProfileBean) obj;
						xml.setAttribute("midletName", a.getMidletName());
						xml.setAttribute("jadPath", a.getJadPath());

						List bookList = a.getBooks();
						if (bookList != null) {
							xml.setAttribute("count", bookList.size());
							for (Iterator iterator = bookList.iterator(); iterator
									.hasNext();) {
								Object bk = (Object) iterator.next();
								xml.add(bk);
							}
						}
					}

				};

				public XMLFormat getFormat(Class cls) {
					if (cls.isAssignableFrom(Book.class)) {
						return BOOK_XML;
					}
					if (cls.isAssignableFrom(ProfileBean.class)) {
						return PROFILE_XML;
					}
					return super.getFormat(cls);
				}

			};

			binding.setAlias(Book.class, "book");
			binding.setAlias(ProfileBean.class, "profile");
		}
		return binding;
	}
}
